<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
/**
 *
 * @author Joker
 *        
 */
class CategoryController extends BaseController
{
    protected $route;
    
    public function index()
    {
        if(!$this->user->can(array('viewEmployCategory','viewTeaches','viewTeachesOrganization'))) abort(405);
        
        $app = Input::get('app','');
        if(empty($app)){
            $reqUri = \Illuminate\Support\Facades\Request::getRequestUri();
            if(str_contains($reqUri,'employ'))
            {
                $app = 'employ';
            }
            elseif(str_contains($reqUri,'teaches') && !str_contains($reqUri, 'organization'))
            {
                $app = 'teaches';
            }
            elseif(str_contains($reqUri, 'organization'))
            {
                $app = 'organization';
            }
        }
        if(empty($app)) $app='employ';
        if($app == 'organization')
        {
            $this->route = 'teaches/'.$app;
        }
        else
        {
            $this->route = $app;
        }
        $category = Category::where(array('pid'=>0,'app'=>$app))->orderBy('display_order')->orderBy('created_at')->get();
        return view("admin.category.list")->with('category',$category)->with('app',$app)->with('route',$this->route);
    }
    
    public function create(Request $req)
    {
        if(!$this->user->can(array('editEmployCategory','editTeaches','editTeachesOrganization'))) abort(405);
        
        $catid=$req->input('catid',0);
        $app = $req->input('app','employ');
        if($app == 'organization')
        {
            $this->route = 'teaches/'.$app;
        }
        else
        {
            $this->route = $app;
        }
        $this->catgory = Category::where(array('app'=>$app,'pid'=>0))->get();
        $cates=array();
        if($catid > 0)
        {
            $cates =Category::where(array('cat_id'=>$catid,'app'=>$app))->first();
        }
        return view("admin.category.create")
                ->with('type',$this->catgory)
                ->with('cates',$cates)
                ->with('app',$app)
                ->with('route',$this->route)
                ->with('blademode','add');
    }
    
    public function edit($catid)
    {
        if(!$this->user->can(array('viewEmployCategory','viewTeaches','viewTeachesOrganization'))) abort(405);
        
        $app = Input::get('app','employ');
        $this->catgory = Category::where(array('app'=>$app,'pid'=>0))->get();  //主分类数据
       // dd( $this->catgory);
        $secondcatid=  Input::get('secondcatid',0);
        $thirdcatid =  Input::get('thirdcatid',0);
        $cate_name = "";
        $cate_sort="";
        $is_index=0;
        $dropdown=0;
        $up = 0;
        $cates=array();
        $second_cates=array();
        $third_cates = array();
        $upcategory= array();
        if($catid > 0)
        {
            $cates =Category::where(array('cat_id'=>$catid,'app'=>$app))->first();
            $cate_name = $cates->cat_name;
            $cate_id = $cates->cat_id;
            $cate_sort = $cates->display_order;
            $identify  =  $cates->identify;
            $description  =  $cates->description;
            $dropdown=$cates->dropdown;
            $is_index=$cates->is_index;
        }
        else
        {
            abort(404);
        }
        if($catid > 0 && $secondcatid > 0)
        {
            $second_cates = Category::where(array('cat_id'=>$secondcatid,'app'=>$app))->first();

            $cate_name = $second_cates->cat_name;
            $cate_id = $second_cates->cat_id;
            $cate_sort = $second_cates->display_order;
            $identify  =  $second_cates->identify;
            $description  =  $second_cates->description;
            $up = 1;
            $dropdown=$second_cates->dropdown;
            $is_index=$second_cates->is_index;
        }
        if($catid > 0 && $secondcatid > 0 && $thirdcatid > 0)
        {
            $third_cates = Category::where(array('cat_id'=>$thirdcatid,'app'=>$app))->first();
            $cate_name = $third_cates->cat_name;
            $cate_id = $third_cates->cat_id;
            $cate_sort = $third_cates->display_order;
            $identify  =  $third_cates->identify;
            $description  =  $third_cates->description;
            $up = 2;
            $upcategory = Category::where(array('pid'=>$cates->cat_id,'app'=>$app))->get();
            $dropdown=$third_cates->dropdown;
            $is_index=$third_cates->is_index;
        }
        if($app == 'organization')
        {
            $this->route = 'teaches/'.$app;
        }
        else
        {
            $this->route = $app;
        }
        return view("admin.category.create")
                ->with('type',$this->catgory)
                ->with('cates',$cates)
                ->with('second_cates',$second_cates)
                ->with('third_cates',$third_cates)
                ->with('cate_id',$cate_id)
                ->with('app',$app)
                ->with('route',$this->route)
                ->with('up',$up)
                ->with('upcategory',$upcategory)
                ->with('cate_name',$cate_name)
                ->with('cate_sort',$cate_sort)
                ->with('identify',$identify)
                ->with('description',$description)
                ->with('blademode','edit')
                ->with('dropdown',$dropdown)
                ->with('is_index',$is_index);

    }
    
    public function store(Request $req)
    {
        if(!$this->user->can(array('editEmployCategory','editTeaches','editTeachesOrganization'))) 
            return $this->formatJson('error','抱歉，您没有相关权限');
        
        $data = $req->only(array('cat_id','cat_name','sort','type','child_1','child_2','child_3','app','identify','description','is_index','dropdown'));


        $route ='';
        if($data['app'] == 'organization')
        {
            $route = 'teaches/'.$data['app'];
        }
        else 
        {
            $route = $data['app'];
        }
        if($data['cat_id'] > 0)
        {
            $category = Category::find($data['cat_id']);
        }
        else
        {
            $category = new Category();
        }
        if(!empty($data['identify'])){
            $data['identify'] = str_replace("，", ",", $data['identify']);
        }
        if(!empty($data['description'])){
            $data['description'] = str_replace("，", ",", $data['description']);
        }
        $category->pid = $this->getTypes($data['type'],$data['child_1'],$data['child_2'],$data['child_3']);
        $category->cat_name = $data['cat_name'];
        $category->display_order = $data['sort'];
        $category->identify = $data['identify'];
        $category->description = $data['description'];
        $category->app = $data['app'];
        $category->is_index = $data['is_index'];
        $category->dropdown = $data['dropdown'];
        $category->save();
        return redirect("admin/".$route."/category");
    }
    
    public function destroy($catid)
    {
        /*判断该分类下是否有子分类*/
        if(!$this->user->can(array('deleteEmployCategory','deleteTeaches','deleteTeachesOrganization')))
            return $this->formatJson('error','抱歉，您没有相关权限');
        
        if ($this->getSubCateByCid($catid))
            return  Response::json(array('status'=>'errors','msg'=>'该分类下还有子分类，不允许删除!'));
        $cat = Category::find($catid);
        $cat->delete();
        return  Response::json(array('status'=>'success','msg'=>'分类：［'.$cat->cat_name.'］ 删除成功'));
    }
}